<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="所属机构">
              <a-select allowClear defaultValue="1" v-model="queryParam.orgId" showSearch :filter-option="filterOption"
                :not-found-content="null">
                <a-select-option v-for="item in orgList" :key="item.id" :value="item.id">
                  {{ item.departName }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="执行执行状态">
              <a-select v-model="queryParam.execStatus" allowClear>
                <a-select-option v-for="(item, index) in execStatus" :key="index" :value="item.value">
                  {{ item.text }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="default" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table ref="table" size="middle" bordered rowKey="id" :columns="columns" :dataSource="dataSource"
        :pagination="ipagination" :loading="loading" class="j-table-force-nowrap" @change="handleTableChange">
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" :disabled="!(record.bizStatus == 0)">编辑</a>
        </span>
        <span slot="execStatus" slot-scope="text">
          <a-tag color="" v-if="text === 0">未执行</a-tag>
          <a-tag color="orange" v-if="text === 1">执行中</a-tag>
          <a-tag color="green" v-if="text === 2">成功</a-tag>
          <a-tag color="red" v-if="text === -1">失败</a-tag>
        </span>
        <span slot="bizStatus" slot-scope="text, record">
          <a-switch v-model="record.bizStatus" @change="handleStatus(record)" />
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->
    <!-- 表单区域 -->
    <PosShopMemberAccountClearModal ref="modalForm" @ok="modalFormOk"></PosShopMemberAccountClearModal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { postAction } from '@/api/manage'
import moment from 'moment'
import PosShopMemberAccountClearModal from './modules/PosShopMemberAccountClearModal'


export default {
  mixins: [JeecgListMixin],
  components: {
    PosShopMemberAccountClearModal,
  },
  data() {
    return {
      description: '积分清零',
      gainDateRange: [],
      useDateRange: [],
      orgList: [],
      columns: [
        {
          title: '单号',
          align: 'center',
          dataIndex: 'billNo',
        },
        {
          title: '适用机构',
          align: 'center',
          dataIndex: 'orgName',
        },
        {
          title: '适用会员',
          align: 'center',
          dataIndex: 'memberScope',
          customRender:(text,record)=>{
            return text == 'ALL' ? '全部会员' : ''
          }
        },
        {
          title: '通知方式',
          align: 'center',
          dataIndex: 'noticeWay',
          customRender:(text,record)=>{
            return text == '2' ? '人工' : ''
          }
        },
        {
          title: '日期前(含当天)过期的积分清零',
          align: 'center',
          dataIndex: 'endDate',

        },
        {
          title: '清零日期',
          align: 'center',
          dataIndex: 'jobDate',

        },
        {
          title: '是否执行',
          dataIndex: 'execStatus',
          align: 'center',
          scopedSlots: { customRender: 'execStatus' },
        },
        {
          title: '是否启用',
          align: 'center',
          dataIndex: 'bizStatus',
          scopedSlots: { customRender: 'bizStatus' },
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark',

        },
        {
          title: '操作人',
          align: 'center',
          dataIndex: 'creator',
        },
        {
          title: '操作时间',
          align: 'center',
          dataIndex: 'updateTime',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
      execStatus: [
        { text: '未执行', value: '0' },
        { text: '已执行', value: '1' },
        { text: '执行中', value: '2' },
      ],
      url: {
        list: '/marketing/posShopMemberAccountClear/pageList',
        edit: '/marketing/posShopMemberAccountClear/edit',
        shopListUrl: '/sys/sysDepart/getMyDepartList',
      },
    }
  },
  computed: {},
  created() {

  },
  methods: {
    moment,
    beforeCreate() {
      this.loadShopList()
    },
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    loadShopList() {
      this.orgList = []
      postAction(this.url.shopListUrl + '?type=2').then((res) => {
        if (res.success) {
          this.orgList = res.result
        }
      })
    },
    handleStatus(record) {
      if(record.execStatus != 0){
        this.$message.warn('该单据状态不允许禁用', 5)
        record.bizStatus = 1
        return
      }

      this.loading = true
      let param = {id:record.id}
      param.bizStatus = record.bizStatus ? 1 : 0
      postAction(this.url.edit, param)
        .then((res) => {
          if (res.success) {
            this.$message.success('操作成功')
          } else {
            this.$message.warn(res.message, 5)
          }
        })
        .finally(() => (this.loading = false))
    },
  },
}
</script>

<style scoped>
@import '~@assets/less/common.less';
</style>